package com.baidu.cmsWeb.contorllers;

import com.baidu.cmsCommes.pojo.Channel;
import com.baidu.cmsCommes.pojo.Group;
import com.baidu.cmsCommes.pojo.Pager;
import com.baidu.cmsCommes.util.PageUtil;
import com.baidu.cmsService.services.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/6/22/15:15
 */
@Controller
@RequestMapping
public class GroupContorller {

    @Autowired
    private GroupService groupService;

    @GetMapping("/admin/group/groups")
    public String groupsList(Model model){
        PageUtil.initPage();
        Pager<Group> pages = groupService.groupListPage(1,10);
        if (pages==null)
            return "error";
        model.addAttribute("datas",pages);
        return "group/list";
    }

    @GetMapping("/admin/group/add")
    public String toSave(Model model){
        model.addAttribute("group",new Group());
        return "group/add";
    }

    @PostMapping("/admin/group/add")
    public String saveGroup(Group group){
        Boolean flag = groupService.saveGroup(group);

        if (!flag)
            return "error";

        return "redirect:/admin/group/groups";
    }

    @GetMapping("/admin/group/delete/{gid}")
    public String deleteGroup(@PathVariable("gid")Integer gid){
        Boolean flag = groupService.deleteGroupById(gid);

        if (!flag)
            return "error";

        return "redirect:/admin/group/groups";
    }

    @GetMapping("/admin/group/update/{gid}")
    public String toUpdate(@PathVariable("gid")Integer gid, Model model){
        Group group = groupService.selectById(gid);

        if (group==null)
            return "error";

        model.addAttribute("group",group);

        return "group/update";
    }

    @PostMapping("/admin/group/update/{gid}")
    public String updateGroup(Group group,@PathVariable("gid")Integer gid){
        Boolean flag = groupService.updateGroupById(group);

        if (!flag)
            return "error";

        return "redirect:/admin/group/groups";
    }

    @GetMapping("/admin/group/clearUsers/{gid}")
    public String clearUsers(@PathVariable("gid")Integer gid){
        Boolean flag = groupService.clearUsers(gid);

        if (!flag)
            return "error";

        return "redirect:/admin/group/groups";
    }

    @GetMapping("/admin/group/listChannels/{gid}")
    public String listChannels(@PathVariable("gid")Integer gid,Model model){
        Group group = groupService.selectById(gid);

        if (group==null)
            return "error";

        model.addAttribute("group",group);

        return "group/listChannel";
    }

    @PostMapping("/admin/group/groupTree/{gid}")
    @ResponseBody
    public List<Channel> showTree(@PathVariable("gid")Integer gid,Model model){
        List<Channel> list = groupService.showTree(gid);

        if (list==null)
            return null;

        model.addAttribute(list);

        return list;
    }

    @GetMapping("/admin/group/setChannels/{gid}")
    public String toSetChannels(@PathVariable("gid")Integer gid,Model model){
        Group group = groupService.selectById(gid);

        if (group==null)
            return "error";

        List<Integer> cids = groupService.getCids(gid);

        if (cids==null)
            return "error";

        model.addAttribute("cids",cids).addAttribute("group",group);

        return "group/setChannel";
    }

    @PostMapping("/admin/channel/treeAll")
    @ResponseBody
    public List<Channel> treeAll(){
        List<Channel> list = groupService.treeAll();
        return list;
    }
}
